import 'package:dali_ng_pera/manager/dnp_cache_manager.dart';
import 'package:dali_ng_pera/manager/dnp_plugin_manager.dart';
import 'package:dali_ng_pera/net/dnp_net_manager.dart';
import 'package:dali_ng_pera/page/browser/dnp_browser_page.dart';
import 'package:dali_ng_pera/page/setting/dnp_setting_page.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'dnp_profile_entity.dart';

class DnpProfileController extends GetxController{

  RefreshController refreshController = RefreshController();

  DnpProfileEntity? profileEntity;

  late BuildContext ctx;

  void requestData() async {
    DnpPluginManager().showLoading();

    var result = await DnpNetManager().get(
      '/haseye/sounds'
    );

    DnpPluginManager().hideLoading();

    refreshController.refreshCompleted();

    if (result.success) {
      profileEntity = DnpProfileEntity.fromJson(result.close);
      DnpCacheManager().saveString("ho_phone", profileEntity?.userInfo?.userphone ?? "");
      update();
    }else {
      DnpPluginManager().showToast(result.kurios ?? "Network Error");
    }
  }

  void clickAtIndex(int idx) {
    if (idx >= (profileEntity!.syllables?.length ?? 0)) {
      return;
    }

    DnpProfileSyllablesEntity entity = profileEntity!.syllables![idx];

    String path = entity.belated ?? "";

    if (path.startsWith("http")) {
      DnpBrowserPage page = DnpBrowserPage(
        path: path,
        needBack: false,
      );
      Navigator.of(ctx).push(MaterialPageRoute(builder: (_) => page));
    }else if (path == "ios://Dali.ngPe.ra/alikelove") {
      Navigator.of(ctx).push(MaterialPageRoute(builder: (_) => DnpSettingPage(phText:profileEntity?.userInfo?.userphone ?? "")));
    }
  }
}